package com.jt;

import com.google.gson.Gson;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class testConnect {
    @Test
    public void test(){
        Jedis jedis = new Jedis("192.168.126.128",6379);
        //jedis.auth("123456");
        jedis.connect();
        String ping = jedis.ping();
        System.out.println(ping);
    }
    /**
     *     //字符串类型练习
     *     1.建立连接
     *     2.存储数据
     *     3.更新数据
     *     4，获取数据
     *     5.释放资源
     *
     */
    @Test
     public void  testString() throws InterruptedException {
        //建立链接(与redis建立链接)
        Jedis jedis = new Jedis("192.168.126.128", 6379);
        //存储数据(key/value)
        jedis.set("id", "10001");
        jedis.set("content", "hello-redis");
        jedis.set("count","10");
        //更新数据
        jedis.incr("count");//对key的值进行自增操作
        jedis.expire("id", 1);//设置key的有效时长
        TimeUnit.SECONDS.sleep(1);//休眠一秒
        //获取数据
        String id = jedis.get("id");
        String content = jedis.get("content");
        String count = jedis.get("count");

        String result=String.format("id=%s,content=%s,count=%s",id,content,count);
        System.out.println(result);//id=null,content=hello-redis,count=11
        //释放资源
        jedis.close();

    }

    /**
     * //json数据练习
     *
     */
    @Test
    public void testJSON2(){
        //1.建立连接
        Jedis jedis = new Jedis("192.168.126.128",6379);
        //2.构建对象
        HashMap<Object, Object> map = new HashMap();
        map.put("id","100");
        map.put("title","spring 认证");
        map.put("content","very good");
        //3.将对象转换为json格式字符串
        Gson gson=new Gson();
        String jsonStr=gson.toJson(map);
        jedis.set("user",jsonStr);
        //4.读取redis中数据
        jsonStr=jedis.get("user");
        System.out.println(jsonStr);//{"id":"100","title":"spring 认证","content":"very good"}

        Map<String,Object> obj=gson.fromJson(jsonStr, Map.class);
        System.out.println(obj);//{id=100, title=spring 认证, content=very good}
        //5.修改map中的数据
        map.put("mobile","2222222222");
        jsonStr= gson.toJson(map);
        jedis.set("user",jsonStr);
        //6.释资源
        jedis.close();


    }

    /**
     * 基于hash类型存储/获取数据
     */

    @Test
    public  void testHash(){
        Jedis jedis = new Jedis("192.168.126.128", 6379);
        //key为member 存储数据
        jedis.hset("member", "id","001");
        jedis.hset("member", "username", "jack");
        jedis.hset("member", "password", "123456");
        //加上更新数据
        jedis.hset("member", "username", "lucy");//lucy
        //获取数据
        String password = jedis.hget("member", "password");
        String username = jedis.hget("member", "username");

        System.out.println(username);//jack-->lucy
        System.out.println(password);//123456
        //
        jedis.close();

    }
    /**
     *  hash练习二：存储map对象
     */
    @Test
    public void testHashOper02(){
        //1.建立连接
        Jedis jedis=new Jedis("192.168.126.128",6379);
        //2.存储一篇博客信息
        Map<String,String> map=new HashMap();
        map.put("x","10");
        map.put("y","20");
        jedis.hset("point",map);
        //3.获取博客内容并输出
        map=jedis.hgetAll("point");
        System.out.println(map);
        //4.释放资源
        jedis.close();
    }

    /**
     *  一、基于list类型操作redis：实现一个秒杀队列
     */
    @Test
     public void testList(){
        //1.连接redis
        Jedis jedis = new Jedis("192.168.126.128", 6379);
        //2.向队列存数据
        jedis.lpush("list1", "A","B","C");
        //3.按先进先出的顺序从队列取数据
        int len =jedis.llen("list1").intValue();//获取列表元素个数
        List<String> list1 = jedis.rpop("list1", len);
        System.out.println(list1);

        jedis.close();

    }
    /**
     *  二、list练习二：实现一个阻塞式队列
     */
    @Test
    public void testListOper02(){
        //1.连接redis
        Jedis jedis=new Jedis("192.168.126.128",6379);
        //2.向队列存数据
        //jedis.lpush("list1","A","B","C");
        //3.按先进先出的顺序从队列取数据
        List<String> list= jedis.brpop(40,"list1");
        System.out.println(list);
        jedis.brpop(40,"list1");
        jedis.brpop(40,"list1");
        jedis.brpop(40,"list1");
        //4.释放资源
        jedis.close();
    }
    /**
     *  set数据类型测试
     */
    @Test
    public  void testSet(){
        Jedis jedis = new Jedis("192.168.126.128",6379);
        jedis.sadd("set1", "ABC","abc","123");

        System.out.println(jedis.scard("set1"));//集合个数
        Set<String> set1 = jedis.smembers("set1");//集合元素
        System.out.println(set1);
        jedis.close();
    }
    //set类型练习
    @Test
    public void testSet2() {
        //1.连接redis
        Jedis jedis = new Jedis("192.168.126.128", 6379);
        //2.朋友圈点赞
       jedis.sadd("counts", "1", "3", "2");

        //3.取出点赞数
        Set<String> set = jedis.smembers("counts");
        System.out.println(set);
        //4.释放资源
        jedis.close();//[1, 2, 3]
    }


}
